package com.lg.product.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.lg.product.entity.SpecDroupParamModel;
import com.lg.product.entity.SpecGroup;
import com.lg.product.entity.SpecParam;
import com.lg.product.service.ISpecGroupService;
import com.lg.product.service.ISpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 规格参数的分组表，每个商品分类下有多个规格参数组 前端控制器
 * </p>
 *
 * @author song
 * @since 2020-07-22
 */
@RestController
@RequestMapping("/spec-group")
public class SpecGroupController {

    @Autowired
    private ISpecGroupService iSpecGroupService;
    @Autowired
    private ISpecParamService iSpecParamService;
    /**
     *
     * 规格 分组 查询
     * @return
     */
    @RequestMapping("groupList")
    public List<SpecGroup> groupListCid(Integer cid){
        List<SpecGroup> specGroups = iSpecGroupService.groupListCid(cid);
        return specGroups;
    }

    /**
     * 规格 分组 添加
     * @return
     */
    @RequestMapping("addSpecGroup")
    public int addSpecGroup(@RequestBody SpecGroup specGroup){
        int i = iSpecGroupService.addSpecGroup(specGroup);

        return i;
    }

    /**
     * 规格 分组 删除
     * @return
     */
    @RequestMapping("deleteSpecGroup")
    public int deleteSpecGroup(Integer id){
        int i = iSpecGroupService.deleteSpecGroup(id);
        long l = id.longValue();
        iSpecParamService.deleteGroupIdParam(l);
        return i;
    }

    /**
     *  规格json数据 回显
     * @param cid
     * @return
     */
    @RequestMapping("groupListCid")
    public Object groupListCid(Long cid){
        SpecDroupParamModel specDroupParamModel = new SpecDroupParamModel();
        specDroupParamModel.setCid(cid.longValue());
        QueryWrapper<SpecGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cid",cid);
        List<SpecGroup> list = iSpecGroupService.list(queryWrapper);
        list.forEach(group->{
            QueryWrapper<SpecParam> paramQueryWrapper = new QueryWrapper<>();
            paramQueryWrapper.eq("group_id",group.getId());
            List<SpecParam> paramlist = iSpecParamService.list(paramQueryWrapper);
            group.setParams(paramlist);
        });
        specDroupParamModel.setGroups(list);
        return specDroupParamModel;
    }
    @RequestMapping("updateParamModel")
    public int updateParamModel(@RequestBody SpecDroupParamModel specDroupParamModel){



        return 1;
    }

    /**
     * 规格 添加 分组和参数
     * @param specDroupParamModel
     * @return
     */
    @RequestMapping("add")
    public int add(@RequestBody SpecDroupParamModel specDroupParamModel){
        System.out.println("======="+specDroupParamModel);
      /* */ Long cid = specDroupParamModel.getCid();
        Integer inte = cid.intValue();
        if(inte==2){
            List<SpecGroup> groups = specDroupParamModel.getGroups();
            groups.forEach(grop->{
                System.out.println("group****"+grop);
                iSpecGroupService.addSpecGroup(grop);
                List<SpecParam> params = grop.getParams();
                params.forEach(par->{
                    par.setCid(grop.getCid());
                    par.setGroupId(grop.getId());
                    System.out.println("=====***"+par);
                    iSpecParamService.addSpecParam(par);
                });
            });
       }
         if (inte==1){
        List<SpecGroup> groups = specDroupParamModel.getGroups();
        groups.forEach(gr->{
            iSpecGroupService.deleteGroupCid(gr.getCid());
            iSpecParamService.deleteParamCid(gr.getCid());
            System.out.println(gr.getCid());
            iSpecGroupService.addSpecGroup(gr);
            List<SpecParam> params = gr.getParams();
            params.forEach(par->{
                par.setCid(gr.getCid());
                par.setGroupId(gr.getId());
                System.out.println("=====***"+par);
                iSpecParamService.addSpecParam(par);
            });
        });
    }
       return 1;
    }

}
